<?php

class RoleController extends MController
{
	public function init()
	{
		$this->foot_js[] = 'js/role.js';
	}
	
	public function actionIndex($p = 1, $preViewRole = false, $role_id = 0)
	{
		if ($preViewRole)
		{
			$this->ajaxRightsList($role_id);
		}
		
		$page_size = 10;
		$RoleModel = new RoleModel();
		//搜索条件
		$condition = $this->getCondition();
		$res = $RoleModel->pager($condition, $page_size);
		$this->var['pages'] = $res['pages'];
		$this->var['res'] = $res['res'];
		
		$this->renderData('index');
	}
	
	public function actionAdd()
	{
		if (Yii::app()->request->isAjaxRequest) {
			//保存权限
			$this->save();
		}
		$this->var['rights'] = Yii::app()->params->rights;
		$this->var['module_rights'] = Yii::app()->params->module_rights;
		
		$this->renderData('add');
	}
	
	public function actionEdit($role_id = 0)
	{
		$role_id = (int) $role_id;
		if ($role_id < 1)
		{
			$this->util->show_404();
		}
		if (Yii::app()->request->isPostRequest) {
			//保存权限
			$this->save($role_id);
		}
		$RoleModel = new RoleModel();
		$role = $RoleModel->findByPk((int) $role_id);
		$rights_list = $role['act_rights'] != '*' ? json_decode($role['act_rights'], true) : $role['act_rights'];
		$this->var['role'] = $role;
		$this->var['rights'] = Yii::app()->params->rights;
		$this->var['module_rights'] = Yii::app()->params->module_rights;
		$this->var['rights_list'] = $rights_list;
		$this->renderPartial('edit', $this->var);
	}
	
	public function actionDel($role_id = 0)
	{
		$role_id = (int) $role_id;
		if ($role_id > 0)
		{
			$RoleModel = new RoleModel();
			$RoleModel->deleteByPk($role_id);
			$this->redirect(BASE_URL . 'role/index');
		}
		$this->util->show_404();
	}
	
	/**
	 * 获取搜索条件
	 */
	private function getCondition()
	{
		$condition = array();
		$keywords = trim(Yii::app()->request->getQuery('keywords'));
		$this->var['keywords'] = $keywords;
		
		if (!empty($keywords))
		{
			$condition = array(
				'condition' => "role_name LIKE :role_name",
				'params' => array(':role_name' => '%' . $keywords . '%')
			);
		}
		return $condition;
	}
	
	/**
	 * 对应角色的权限列表
	 * @param int $role_id
	 */
	private function ajaxRightsList($role_id)
	{
		if (Yii::app()->request->isAjaxRequest) {
			$RoleModel = new RoleModel();
			$role = $RoleModel->findByPk((int) $role_id);
			$rights_list = $role['act_rights'] != '*' ? json_decode($role['act_rights'], true) : $role['act_rights'];
			$this->var['rights'] = Yii::app()->params->rights;
			$this->var['module_rights'] = Yii::app()->params->module_rights;
			$this->var['rights_list'] = $rights_list;
			$this->renderPartial('rights_list', $this->var);
			die;
		}
		$this->util->show_404();
	}
	
	/**
	 * 保存权限
	 * 私有方法只能通过曝光的动作来调用
	 */
	private function save($role_id = 0) 
	{
		$role_name = trim(Yii::app()->request->getPost('role_name'));
		$rights = Yii::app()->request->getPost('rights');
		if (empty($role_name))
		{
			$this->util->return_json('权限名称不能为空', false);
		}
		
		$RoleModel = new RoleModel();
		if ($RoleModel->role_save($role_name, $rights, $role_id))
		{
			$this->util->return_json('保存成功', true, BASE_URL . 'role/index');
		}
		else
		{
			$this->util->return_json('保存失败', false);
		}
	}
}